package com.example.administrator.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.IntDef;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {
    private static final String TAG="service";
    MediaPlayer m;

    public IBinder onBind(Intent intent) {
        Log.e(TAG, "onBind: " );
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    Intent intent1 = new Intent();
                    intent1.setAction("bfq");
                    int currentPosition = m.getCurrentPosition();
                    intent1.putExtra("currentPosition",currentPosition);
                    int duration = m.getDuration();
                    intent1.putExtra("duration",duration);
                    Log.e(TAG, "run: "+currentPosition+"----"+duration);
                    sendBroadcast(intent1);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        return new mybinder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e(TAG, "onUnbind: " );
        return super.onUnbind(intent);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        m = MediaPlayer.create(MyService.this, R.raw.qinglang);
        Log.e(TAG, "onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand: ");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy: " );
    }

    class mybinder extends Binder {

        public void play(){
            m.start();
            Log.e(TAG, "play: ");
        }

        public void pause() {
            m.pause();
        }

        public void end() {
            m.reset();
        }
    }


}
